
/*
 * GameCreatorPanel.java
 *
 * Created on 2011-05-13, 11:53:28
 */
package battleship;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Tomek
 */
public class GameCreatorPanel extends javax.swing.JPanel {

	protected JTable table;
	protected MainFrame parent;
	protected Player actualPlayer;
	protected SetShipFieldActionListener sSFAL;
	protected ActionListener doneButtonActionListener = new java.awt.event.ActionListener() {
		@Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        doneButtonActionPerformed(evt);
    }
	};
	protected ActionListener doneButtonEndSet = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent ae) {
			if (((MyTableModel) shipsInDocks.getModel()).getRowCount() == 0) {
				actualPlayer.table = new MyTableModel(parent.rules.shipsInGame);
				if (actualPlayer != parent.rules.player2) {
					GameSteer.nextPlayerSetsHisShips();
				} else {
					parent.clearValidateAndRepaintAll();
					Battleship battle = new Battleship(parent, parent.rules);
					battle.startGame();
				}
			} else {
				shipsInDocks.setEnabled(true);
				doneButton.setEnabled(false);
				actualPlayer.grid.repaint();
			}
		}
	};
	/** Creates new form GameCreatorPanel */
	public GameCreatorPanel(JTable jt, MainFrame p, Player actPlayer) {
		super();
		table = jt;
		parent = p;
		sSFAL = GameSteer.sSFAL;
		actualPlayer = actPlayer;
		sSFAL.gcp = this;
		initComponents();
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    shipsLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    shipsInDocks = new JTable(new MyTableModel(table.getModel()));
    doneButton = new javax.swing.JButton();
    PlayerNameLabel = new javax.swing.JLabel();
    randomButton = new javax.swing.JButton();

    shipsLabel.setText("Ships");

    shipsInDocks.setRowSelectionInterval(0, 0);
    shipsInDocks.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e){
        if(shipsInDocks.getSelectedRow()!=-1){
          actualPlayer.grid.setButtonsEnabled(true);
        }else{
          if(shipsInDocks.getRowCount()>0){
            shipsInDocks.setRowSelectionInterval(0, 0);
          }else
          actualPlayer.grid.setButtonsEnabled(false);
        }
      }
    });
    shipsInDocks.getTableHeader().setReorderingAllowed(false);
    jScrollPane1.setViewportView(shipsInDocks);

    doneButton.addActionListener(doneButtonActionListener);
    doneButton.addActionListener(doneButtonEndSet);
    doneButton.setText("Done");
    doneButton.setEnabled(false);

    PlayerNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    PlayerNameLabel.setText(actualPlayer.getName());

    randomButton.setText("Random");
    randomButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        randomButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGroup(layout.createSequentialGroup()
        .addGap(36, 36, 36)
        .addComponent(shipsLabel))
      .addComponent(PlayerNameLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(randomButton, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
        .addContainerGap())
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(doneButton, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(PlayerNameLabel)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(shipsLabel)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(doneButton)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
        .addComponent(randomButton))
    );
  }// </editor-fold>//GEN-END:initComponents

		private void randomButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomButtonActionPerformed
			if(shipsInDocks.getRowCount()==0){
				actualPlayer.grid.setFieldsType(FieldType.NORMAL);
				actualPlayer.table = new MyTableModel(parent.rules.shipsInGame);
				shipsInDocks.setModel(new MyTableModel(actualPlayer.table));
				actualPlayer.ships.clear();
			}
			actualPlayer.grid.setButtonsEnabled(false);
			actualPlayer.setShips((MyTableModel)shipsInDocks.getModel());
			((MyTableModel)shipsInDocks.getModel()).removeShips();
			parent.validateAndRepaintAll();
			doneButton.removeActionListener(doneButtonActionListener);
			doneButton.setEnabled(true);
		}//GEN-LAST:event_randomButtonActionPerformed
	private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {
			for (Field f : sSFAL.set) {
				f.fieldType = FieldType.SHIP;
			}
			for (Field f : sSFAL.set) {
				for (Field g : parent.rules.corneringAllowed ? actualPlayer.grid.getNeighbours(f) : actualPlayer.grid.getNeighboursWithCorners(f)) {
					if (g.fieldType == FieldType.NORMAL) {
						g.fieldType = FieldType.INACTIVE;
					}
				}
			}
			MyTableModel dTM = ((MyTableModel) shipsInDocks.getModel());
			dTM.removeOneShip(sSFAL.actualShipSize);
			actualPlayer.ships.add(new Ship(sSFAL.actualShipSize, sSFAL.set));
			sSFAL.set.clear();
			doneButtonEndSet.actionPerformed(evt);
		}
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel PlayerNameLabel;
  protected javax.swing.JButton doneButton;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JButton randomButton;
  protected javax.swing.JTable shipsInDocks;
  private javax.swing.JLabel shipsLabel;
  // End of variables declaration//GEN-END:variables
}
